package com.goldsign.pay.provider.accountbase.service.impl;

import com.goldsign.pay.common.log.SysLog;
import com.goldsign.pay.common.util.Constant;
import com.goldsign.pay.provider.accountbase.dao.AccountbaseDao;
import com.goldsign.pay.provider.accountbase.service.AccountBaseService;
import com.goldsign.pay.provider.accountbase.to.CreateAccountReqDTO;
import com.goldsign.pay.provider.accountbase.vo.AccountInfoDO;
import com.goldsign.pay.common.exception.GcServiceException;
import com.goldsign.pay.common.string.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AccountBaseServiceImpl implements AccountBaseService {
	
	@Autowired
	AccountbaseDao dao;

	@Override
	public void createAccount(CreateAccountReqDTO req, String accountNo) throws GcServiceException {
		try {
			//生成用户信息
			AccountInfoDO accountInfo = new AccountInfoDO();
			
			accountInfo.setUser_no(req.getUser_no());
			accountInfo.setAccount_no(accountNo);
			accountInfo.setCurrency_type(StringUtil.isEmpty(req.getCurrency_type(), "CNY"));
			accountInfo.setAccount_type(StringUtil.isEmpty(req.getAccount_type(), "0"));
			accountInfo.setAccount_status(StringUtil.isEmpty(req.getAccount_status(), "00"));
			accountInfo.setAccount_catego(req.getAccount_catego());
			accountInfo.setBalance("0");
			accountInfo.setFreeze_amount("0");
			accountInfo.setOverdraw_flag(StringUtil.isEmpty(req.getOverdraw_flag(), "0"));
			accountInfo.setOverdraw_amount(StringUtil.isEmpty(req.getOverdraw_amount(), "0"));
			
			dao.insertAccountInfo(accountInfo);
			
		} catch (Exception e) {
			SysLog.error(req+"_"+ accountNo+"+" + e.getMessage());
			throw new GcServiceException("ACCOUNTBASE_SYSTERM_ERROR", Constant.ACCOUNTBASE_SYSTERM_ERROR);
		}
	}

	@Override
	public AccountInfoDO getAccountInfo(String accountNo) throws GcServiceException {
		try {
			AccountInfoDO accountInfo = dao.getAccountInfo(accountNo);
			if(accountInfo == null) {
				throw new GcServiceException("ACCOUNT_NO_IS_NOT_EXISTS",Constant.ACCOUNT_NO_IS_NOT_EXISTS);
			}
			return accountInfo;
		} catch (GcServiceException e) {
			throw e;
		} catch (Exception e) {
			SysLog.error(accountNo +"_"+e.getMessage());
			throw new GcServiceException("ACCOUNTBASE_SYSTERM_ERROR",Constant.ACCOUNTBASE_SYSTERM_ERROR);
		}
	}

	@Override
	public List<AccountInfoDO> listAccountInfo(String userNo) throws GcServiceException {
		try {
			return dao.listAccountInfo(userNo);
		} catch (Exception e) {
			SysLog.error(userNo +"_"+e.getMessage());
			throw new GcServiceException("ACCOUNTBASE_SYSTERM_ERROR",Constant.ACCOUNTBASE_SYSTERM_ERROR);
		}
	}

}
